package com.product.controller;

import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.product.entity.AttrEntity;
import com.product.entity.ProductAttrValueEntity;
import com.product.service.AttrService;
import com.product.service.ProductAttrValueService;
import com.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品属性
 *
 * @author mxl
 * @email 3299686223@qq.com
 * @date 2022-08-08 14:42:26
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions(":attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions(":attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions(":attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":attr:delete")
    public R delete(@RequestBody List<Long> attrIds) {
        attrService.removeByIds(attrIds);

        return R.ok();
    }

    @RequestMapping("/base/list")
    public R baseList(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryBaseAttrPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询商品规格值
     *
     * @param spuId 商品id
     * @return 商品属性值
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrList(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseListForSpu(spuId);
        return R.ok().put("data", entities);
    }

    /**
     * 修改商品规格
     *
     * @param spuId 商品id
     * @return 结果
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }
}
